const fs = require('fs');
const path = require('path');
const sizeOf = require('image-size');

function getImageSize(imagePath) {
    try {
        // 试图获取图像尺寸
        const dimensions = sizeOf(imagePath);
        return dimensions;
    } catch (error) {
        console.error(`An error occurred while attempting to get the image size: ${error.message}`);
        return { width: 0, height: 0 };
    }
}

function imageToBase64(filePath) {
    // 读取图片文件
    const imageBuffer = fs.readFileSync(filePath);
    // 转换为 Base64 编码
    const base64Data = imageBuffer.toString('base64');
    // 构造 Base64 URL
    const imageType = path.extname(filePath).slice(1); // 获取图片类型（如 png, jpeg）
    return `data:image/${imageType};base64,${base64Data}`;
}

module.exports = { getImageSize, imageToBase64 };
